package com.bridge.client.bridgedynamcclient.custom_compinents;

import com.bridge.client.bridgedynamcclient.utils.FXMLUtil;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.skin.PaginationSkin;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

import java.util.List;

public class TextImageView extends ScrollPane {

    private VBox vBox=new VBox();

    public TextImageView(double height, double width) {
        setPrefSize(width, height);
        vBox.setPrefSize(width,height);
        vBox.setAlignment(Pos.TOP_CENTER);
        vBox.setSpacing(10);
        setContent(vBox);
        initialize();
    }

    private void initialize() {

    }

    private void addView(ImageData imageData, double imageHeight, double imageWidth) {
        Text text = new Text(imageData.getTitle());
        text.setStyle("-fx-font-weight: bold;-fx-font-size: 16; ");
        ImageView imageView=null;
        if(imageData.getType()==1){
            //本地资源
            imageView  = FXMLUtil.getImageView(imageData.getResUrl(), imageHeight, imageWidth);
        }else {
            //网络资源，需要异步处理
            imageView  = FXMLUtil.getImageViewByUrl(imageData.getResUrl(), imageHeight, imageWidth);
        }
        imageView.smoothProperty().setValue(false);
        imageView.cacheProperty().setValue(true);

        VBox.setMargin(text,new Insets(10,0,10,0));
        vBox.getChildren().addAll(imageView,text);
    }

    public void setImages(List<ImageData> list, double imageHeight, double imageWidth) {
        for (ImageData imageData : list) {
            addView(imageData, imageHeight, imageWidth);
        }
    }


    public static class ImageData {
        private String title;
        private String resUrl;

        private Integer type;

        public ImageData(String title, String resUrl, Integer type) {
            this.title = title;
            this.resUrl = resUrl;
            this.type = type;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getResUrl() {
            return resUrl;
        }

        public void setResUrl(String resUrl) {
            this.resUrl = resUrl;
        }

        public Integer getType() {
            return type;
        }

        public void setType(Integer type) {
            this.type = type;
        }
    }
}
